package leetcode.D200.T139;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        Set<String> set = new HashSet<>(wordDict);
        boolean[] dp = new boolean[s.length()+1];
        dp[0] = true;
        for (int i=0; i<s.length(); ++i) {
            for (int j=i+1; j<=s.length(); ++j) {
                String str = s.substring(i, j);
                if (set.contains(str) && dp[i]) {
                    dp[j] = true;
                }
            }
        }
        return dp[s.length()];
    }
}